package com.example.first.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.first.entity.User;
import com.example.first.mapper.UserMapper;
import com.example.first.service.impl.UserServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.*;
import java.util.function.Function;
import java.util.stream.Collectors;
import java.util.stream.Stream;

@RestController
@RequestMapping("/user")
public class UserController {
    @Resource
    private UserMapper userMapper;
    @Autowired
    private UserServiceImpl userService;
    @GetMapping
    public List<User> findAll(){
        return userMapper.findAll();
    }
    //queryWrapper等值查询,相当于select * from user where name='张三'
    @GetMapping("/find")
    public List<User> find(){
        QueryWrapper<User> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("name","张三");
        return userMapper.selectList(queryWrapper);
    }
    @GetMapping("/like")
    public List<User> findLike(){
        QueryWrapper<User> queryWrapper=new QueryWrapper<>();
        queryWrapper.like("name","张三");
        return userMapper.selectList(queryWrapper);
    }
    @GetMapping("more")
    public List<User> findMore(){
        QueryWrapper<User> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("age",20).ne("identity",1515);
        return userMapper.selectList(queryWrapper);
    }

    @GetMapping("/findPage")
    public Page<User> findPage(@RequestParam Integer pageNum, Integer pageSize){
        QueryWrapper<User> queryWrapper=new QueryWrapper<>();
        queryWrapper.orderByDesc("id");
        Page<User> page = userService.page(new Page<>(pageNum, pageSize),queryWrapper);
        return page;
    }
    /*
    * 多条件查询*/
    @GetMapping("/findList")
    public List<User> findList(){
       QueryWrapper queryWrapper=new QueryWrapper();
       queryWrapper.likeRight("name","王");
       queryWrapper.between("age",10,30);
       queryWrapper.isNotNull("phone");
        List list = userMapper.selectList(queryWrapper);
        return  list;
    }
    @GetMapping("/findListMore")
    public List<User> findListMore(){
        QueryWrapper<User> queryWrapper=new QueryWrapper<>();
        queryWrapper.likeRight("name","郭")
                .or()
                .isNotNull("phone")
                .orderByDesc("age");
        List<User> list=userMapper.selectList(queryWrapper);
        return list;
    }
    @GetMapping("/stream")
    public Stream<User> findStream(){
        List<User> list = userService.list();
        Stream<User> userStream = list.stream().filter(user -> StringUtils.equals("郭三", user.getName()));
        return userStream;
    }

    @PostMapping
    public boolean save(@RequestBody User user){
        return userService.saveUser(user);
    }
    @DeleteMapping("/{id}")
    public Integer delete(@PathVariable Integer id){
        return userMapper.deleteById(id);
    }
    @GetMapping("/page")
    public Map<String, Object> findPage(@RequestParam Integer pageNum,
                                        Integer pageSize,
                                        String name){
        pageNum=(pageNum-1)*pageSize;
        List<User> data=userMapper.selectPage(pageNum,pageSize,name);
        Integer total= userMapper.selectTotal(name);
        Map<String,Object> res=new HashMap<>();
        res.put("data",data);
        res.put("total",total);
        return  res;
    }
    @GetMapping("/stream/map")
    public Map<Integer,String> findMap(){
        List<User> list =userService.list();
        Map<Integer, String> collect = list.stream().collect(Collectors.toMap(User::getAge, User::getName));
        return collect;
    }
    @GetMapping("/stream/listToMap")
    public Map<Integer, User> listToMap(){
        List<User> list = userService.list();
        // 2.根据age作为key，user对象作为value转map（age相同时前面覆盖后面的数据）
        Map<Integer, User> collect = list.stream().collect(Collectors.toMap(User::getAge, Function.identity(), (key1, key2) -> key2));
        return collect;

    }

    @PostMapping("/getById/{id}")
    public User getById(@PathVariable Integer id){
        return userService.getById(id);
    }
    @GetMapping("/getOne")
    public User getOne(){
        QueryWrapper<User> queryWrapper=new QueryWrapper<>();
        queryWrapper.
                select("id","name","identity")
                .likeRight("name","张")
                .last("LIMIT 1");
        return userService.getOne(queryWrapper,false);

    }
    @GetMapping("/getMap")
    public Map<String, Object> getMap(){
        QueryWrapper<User> queryWrapper=new QueryWrapper<>();
        queryWrapper.select("id","name","identity");
        return userService.getMap(queryWrapper);

    }




}
